﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using SAC.Helper;
using DAL;
using System.Web;

namespace BLL
{
    public class BLLBwjc
    {
        ArrayList list = new ArrayList();
        StringBuilder sb = new StringBuilder();
        StrHelper sh = new StrHelper();
        DALData da = new DALData();
        DALBwjc daBw = new DALBwjc();

        public string retBwjcByDate(string type, string date, string unit)
        {
            string qsrq = "";
            string jsrq = "";

            string[] zb = null;
            string[] data = null;

            DateTime dtNow = DateTime.Now;

            if (type == "rb")
            {
                qsrq = DateTime.Parse(date).ToString("yyyy-MM-dd 0:00:00");

                if (DateTime.Parse(date).Year == dtNow.Year && DateTime.Parse(date).Month == dtNow.Month && DateTime.Parse(date).Day == dtNow.Day)
                    jsrq = dtNow.ToString("yyyy-MM-dd H:00:00");
                else
                    jsrq = DateTime.Parse(date).ToString("yyyy-MM-dd 23:59:59");
            }
            else
            {
                qsrq = DateTime.Parse(date).ToString("yyyy-MM-01 0:00:00");

                if (DateTime.Parse(date).Year == dtNow.Year && DateTime.Parse(date).Month == dtNow.Month)
                    jsrq = dtNow.ToString("yyyy-MM-dd H:00:00");
                else
                    jsrq = sh.LastDayOfMonth(DateTime.Parse(date)).ToString("yyyy-MM-dd 23:59:59");
            }

            list = daBw.retBwjcByDate(qsrq, jsrq, unit);

            if (list != null && list.Count > 0)
            {
                zb = (string[])list[0];
                data = (string[])list[1];

                for (int i = 0; i < zb.Length; i++)
                {
                    sb.Append("<tr>");

                    sb.AppendFormat(" <td rowspan=\"3\" style=\"width:12%;\"><div align=\"center\">{0}</td>", zb[i].ToString());

                    string[] arr = data[i].ToString().Split('&');

                    string[] arrH1 = arr[0].ToString().Split('+');

                    sb.AppendFormat("<td style=\"width:5%;\"><div align=\"center\">{0}</td>", arrH1[0]);

                    string[] arrValH1 = arrH1[1].ToString().Split(';');

                    for (int j = 0; j < arrValH1.Length - 1; j++)
                    {
                        string[] strVal = arrValH1[j].Split(',');

                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[0].ToString());
                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[1].ToString());
                    }
                    sb.Append("</tr>");
                    sb.Append("<tr>");

                    string[] arrH2 = arr[1].ToString().Split('+');

                    sb.AppendFormat("<td style=\"width:5%;\"><div align=\"center\">{0}</td>", arrH2[0]);

                    string[] arrValH2 = arrH2[1].ToString().Split(';');

                    for (int j = 0; j < arrValH2.Length - 1; j++)
                    {
                        string[] strVal = arrValH2[j].Split(',');

                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[0].ToString());
                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[1].ToString());
                    }
                    sb.Append("</tr>");

                    sb.Append("<tr>");

                    string[] arrH3 = arr[2].ToString().Split('+');

                    sb.AppendFormat("<td style=\"width:5%;\"><div align=\"center\">{0}</td>", arrH3[0]);

                    string[] arrValH3 = arrH3[1].ToString().Split(';');

                    for (int j = 0; j < arrValH3.Length - 1; j++)
                    {
                        string[] strVal = arrValH3[j].Split(',');

                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[0].ToString());
                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[1].ToString());
                    }
                    sb.Append("</tr>");
                }
            }
            else
                sb.Append("出错了.........");

            return sb.ToString();
        }

        public string retBwjcByDateByFD(string type, string date, string unit)
        {
            string qsrq = "";
            string jsrq = "";

            string[] zb = null;
            string[] data = null;

            DateTime dtNow = DateTime.Now;

            if (type == "rb")
            {
                qsrq = DateTime.Parse(date).ToString("yyyy-MM-dd 0:00:00");

                if (DateTime.Parse(date).Year == dtNow.Year && DateTime.Parse(date).Month == dtNow.Month && DateTime.Parse(date).Day == dtNow.Day)
                    jsrq = dtNow.ToString("yyyy-MM-dd H:00:00");
                else
                    jsrq = DateTime.Parse(date).ToString("yyyy-MM-dd 23:59:59");
            }
            else
            {
                qsrq = DateTime.Parse(date).ToString("yyyy-MM-01 0:00:00");

                if (DateTime.Parse(date).Year == dtNow.Year && DateTime.Parse(date).Month == dtNow.Month)
                    jsrq = dtNow.ToString("yyyy-MM-dd H:00:00");
                else
                    jsrq = sh.LastDayOfMonth(DateTime.Parse(date)).ToString("yyyy-MM-dd 23:59:59");
            }

            list = daBw.retBwjcByDateByFD(qsrq, jsrq, unit);

            if (list != null && list.Count > 0)
            {
                zb = (string[])list[0];
                data = (string[])list[1];

                for (int i = 0; i < zb.Length; i++)
                {
                    sb.Append("<tr>");

                    sb.AppendFormat(" <td rowspan=\"5\" style=\"width:12%;\"><div align=\"center\">{0}</td>", zb[i].ToString());

                    string[] arr = data[i].ToString().Split('&');

                    string[] arrH3 = arr[2].ToString().Split('+');

                    sb.AppendFormat("<td style=\"width:5%;\"><div align=\"center\">{0}</td>", arrH3[0]);

                    string[] arrValH3 = arrH3[1].ToString().Split(';');

                    for (int j = 0; j < arrValH3.Length - 1; j++)
                    {
                        string[] strVal = arrValH3[j].Split(',');

                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[0].ToString());
                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[1].ToString());
                    }
                
                    sb.Append("</tr>");

                    sb.Append("<tr>");

                    string[] arrH2 = arr[1].ToString().Split('+');

                    sb.AppendFormat("<td style=\"width:5%;\"><div align=\"center\">{0}</td>", arrH2[0]);

                    string[] arrValH2 = arrH2[1].ToString().Split(';');

                    for (int j = 0; j < arrValH2.Length - 1; j++)
                    {
                        string[] strVal = arrValH2[j].Split(',');

                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[0].ToString());
                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[1].ToString());
                    }
                    sb.Append("</tr>");

                    sb.Append("<tr>");
                    //
                    string[] arrH1 = arr[0].ToString().Split('+');

                    sb.AppendFormat("<td style=\"width:5%;\"><div align=\"center\">{0}</td>", arrH1[0]);

                    string[] arrValH1 = arrH1[1].ToString().Split(';');

                    for (int j = 0; j < arrValH1.Length - 1; j++)
                    {
                        string[] strVal = arrValH1[j].Split(',');

                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[0].ToString());
                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[1].ToString());
                    }
                   
                    sb.Append("</tr>");

                    sb.Append("<tr>");

                    string[] arrH5 = arr[4].ToString().Split('+');

                    sb.AppendFormat("<td style=\"width:5%;\"><div align=\"center\">{0}</td>", arrH5[0]);

                    string[] arrValH5 = arrH5[1].ToString().Split(';');

                    for (int j = 0; j < arrValH5.Length - 1; j++)
                    {
                        string[] strVal = arrValH5[j].Split(',');

                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[0].ToString());
                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[1].ToString());
                    }
                    sb.Append("</tr>");
                   
                    sb.Append("<tr>");

                    string[] arrH4 = arr[3].ToString().Split('+');

                    sb.AppendFormat("<td style=\"width:5%;\"><div align=\"center\">{0}</td>", arrH4[0]);

                    string[] arrValH4 = arrH4[1].ToString().Split(';');

                    for (int j = 0; j < arrValH4.Length - 1; j++)
                    {
                        string[] strVal = arrValH4[j].Split(',');

                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[0].ToString());
                        sb.AppendFormat("<td><div align=\"center\">{0}</td>", strVal[1].ToString());
                    }
                    sb.Append("</tr>");
                }
            }
            else
                sb.Append("出错了.........");

            return sb.ToString();
        }
    }
}
